package Dao.imp;

import entity.Cart;
import mapper.CartMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class CartDao implements CartMapper {
    private SqlSessionFactory sqlSessionFactory;

    public CartDao() {
        sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    }

    @Override
    public int addCart(Cart cart) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.insert("addCart", cart);
            session.commit();
        }
        return result;
    }

    @Override
    public int deleteCart(int cid) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.delete("deleteCart", cid);
            session.commit();
        }
        return result;
    }

    @Override
    public int updateCart(Cart cart) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.update("updateCart", cart);
            session.commit();
        }
        return result;
    }

    @Override
    public List<Cart> queryCartByUid(int uid) {
        List<Cart> list = null;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            list = session.selectList("queryCartByUid", uid);
        }
        return list;
    }
}
